package com.lincoln.framework.bean;

import com.lincoln.framework.interfaces.BaseExecute;
import com.lincoln.framework.interfaces.BaseProtocol;
import org.slf4j.Logger;

/**
 * 带消息号的执行器
 *
 * @author lincoln
 */
public abstract class AbstractMsgNoExecute extends BaseExecute {
    /**
     * 服务器发过去的
     *
     * @return
     */
    @Override
    public abstract byte[] getServerCmd();

    /**
     * 客户端发过来的
     *
     * @return
     */
    @Override
    public abstract byte[] getClientCmd();

    /**
     * 日志
     *
     * @return
     */
    @Override
    public abstract Logger getLogger();

    /**
     * 解析客户端发过来的消息
     *
     * @param protocol
     */
    @Override
    public boolean decoder(BaseProtocol protocol) {
        return true;
    }

    /**
     * 执行解析+回调任务
     *
     * @return
     */
    @Override
    public boolean execute(BaseProtocol protocol) {
        return true;
    }

    /**
     * 发送一个消息给客户端或者decoder处理后调用
     *
     * @param payload
     * @return
     */
    @Override
    protected boolean send(byte[] payload) {
        try {
            AbstractMsgNoProtocol reply = new AbstractMsgNoProtocol(getServerCmd(), payload);
            send(reply);
            return true;
        } catch (Exception e) {
            getLogger().error("", e);
            return false;
        }
    }

    /**
     * 发送一个消息给客户端或者decoder处理后调用
     *
     * @return
     */
    @Override
    protected boolean send() {
        try {
            BaseProtocol protocol = new AbstractMsgNoProtocol(getServerCmd(), null);
            return send(protocol);
        } catch (Exception e) {
            getLogger().error("发送出错:", e);
            return false;
        }
    }
}
